import subprocess
import json
import os
from args import *
def send_post_request():
    curl_command = f'''
    curl -X POST {model_url}/model/chat \
         -H "Content-Type: application/json" \
         -d@data.json
    '''
    try:
        result = subprocess.run(curl_command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        output = result.stdout
        output = json.loads(output)
        print("Response:\n", json.dumps(output, ensure_ascii=False, indent=4))
    except subprocess.CalledProcessError as e:
        print("Error occurred:", e.stderr)

# 调用函数

def run_process(curl_command):
    result = subprocess.run(curl_command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                            text=True)
    return result


def get_group():
    curl_command = f'''curl -X GET {in_data_url}/agg?size=10'''
    result = run_process(curl_command)
    output = result.stdout
    output = json.loads(output)
    return output
output = get_group()
group = output["groups"]["cluster_group_mid"]["data"]
data_path = "data"
group_ids = [list(g.items())[0][0] for g in group if list(g.items())[0][1] > 1]
for id_ in group_ids:
    run_process("mkdir -p " + os.path.join(data_path, id_))

def wrap(id_):
    curl_command = f'curl -X GET "{in_data_url}/get?newsClusterId={id_}&getField=cluster_id3&size=1000&keywords=false"'
    return curl_command
for id_ in group_ids:
    w_id = wrap(id_)
    output = run_process(w_id).stdout
    try:
        res = json.loads(output)
        res = [i['content'] for i in res]
        with open(os.path.join(data_path, id_, "original"), "w") as f:
            json.dump(res, f, ensure_ascii=False, indent=4)
    except:
        print("wrong: " + id_)